<?php
if (!isset($_SESSION)) {
	session_start();
	if(!isset($_SESSION['username'])){
		$_SESSION['username']='';
	}
}

if (!empty($_GET))
{
	$action=$_GET['action'];
	if($action=='logout') 
	{ 
		@session_destroy(); 
		header("Location:/");
	}
}

if (!empty($_POST))
{
	$username = trim($_POST['username']);
	$pwd = md5($_POST['pwd']);
	
	require_once('lib/connbae.php');

	$sql = "SELECT username FROM Author WHERE username='$username' AND pwd='$pwd' LIMIT 1";

	$result =mysql_query($sql);
	
	if(mysql_fetch_array($result))
	{
		mysql_close();
		$_SESSION['username'] = $username;
		if (isset($_GET['action'])){
			$action=$_GET['action'];
			if($action=='login') {
				header("Location:/");
			}else if($action=='ask'){
				header("Location:ask.php");
			}else if($action=='answer'){
				if (isset($_GET['q'])){
					$q=$_GET['q'];
					header("Location:answer.php?q='$q'");
				}
				header("Location:/");
			}
			header("Location:/");
		}
		header("Location:/");		
	}
	mysql_close();
	exit('操作失败,<a href="javascript:history.back(-1);">返回</a>');	
}
?>
<!DOCTYPE HTML>
<html>
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>C</title>
	<link rel="stylesheet" type="text/css" href="css/base.css"/>
	<link rel="stylesheet" type="text/css" href="css/login.css"/>
	<link href="img/favour.ico" type="image/ico" rel="shortcut icon">
	<script type="text/javascript" src="#"></script>
</head>
<body>
	<div id="content">
		<div id="container">
			<div id="header">
				<div class="nav">
					<ul>
						<li><a class="logo" href="/" title="Funny C"></a></li>
						<li><a href="/">主页</a></li>
						<li><a href="ask.php">提问</a></li>
						<li><a href="documation.php">资源</a></li>
<?php if(!isset($_SESSION['username'])||$_SESSION['username']=='')
					{
echo "						<li><a href='login.php?action=login'>登录</a></li>
						<li><a href='register.php'>注册</a></li>";
					}else{
echo "						<li><a href='my.php'>个人中心</a></li>
						<li><a href='login.php?action=logout'>注销</a></li>\n";
					} ?>
					</ul>
				</div>	
			</div><hr/>
			<div id="login">
				<form id="frmlogin" method="post" action='login.php?action=<?php echo$action;?>'>
					<p>
						<label for="username" class="tblabel">用户名</label>
						<input type="text" size="12" value="" id="username" name="username" class="tbinput"/>
					</p><hr class="clear"/>
					<p>
						<label for="pwd" class="tblabel">密　码</label>
						<input type="text" size="12" value="" id="pwd" name="pwd" class="tbinput"/>
					</p><hr class="clear"/>
					<p>
						<input type="submit" value="提交" id="submit" name="submit" class="submit"/>
						<a href="register.php" class="submit">注册</a>
					</p><hr class="clear"/>
				</form>
			</div>
		</div>
		<div id="footer">
			<p>©2014 C语言在线答疑系统 v 1.0</p>	
		</div>			
	</div>
</body>
</html>